﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;

namespace Kokomo.Controls
{
	public class DelegatedTemplateSelector : DataTemplateSelector
	{
		public DelegatedTemplateSelector(Func<Type, DataTemplate> selectorDelegate)
		{
			if (selectorDelegate == null) throw new ArgumentNullException("selectorDelegate");
			this.SelectorDelegate = selectorDelegate;
		}
		public Func<Type, DataTemplate> SelectorDelegate { get; private set; }
		public override DataTemplate SelectTemplate(object item, DependencyObject container)
		{
			DataTemplate template = null;
			if (item != null)
			{
				Type type = item.GetType();
				template = this.SelectorDelegate(type);
			}
			if (template == null)
			{
				template = base.SelectTemplate(item, container);
			}

			return template;
		}
	}
}
